<?php

namespace common\models;

use app\models\AdminLog;
use Yii;

/**
 * This is the model class for table "Module".
 *
 * @property string $id
 * @property string $name
 * @property string $content
 */
class Module extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '`module`';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name'], 'required'],
            [['content'], 'string'],
            [['name'], 'string', 'max' => 64],
            [['name'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '模块名称',
            'content' => '内容',
        ];
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
        if ($insert) {
            AdminLog::log("新添自定义模块：" . $this->name, "create");
        } else {
            AdminLog::log("更新自定义模块：旧：" . $changedAttributes['name'] . "；新：" . $this->name);
        }
    }

    public function afterDelete()
    {
        parent::afterDelete();
        AdminLog::log("删除自定义模块：" . $this->name, "delete");
    }
}
